/**
 * @(#)SimulatorTaskServiceImpl.java 2017年2月24日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.jsbd.boss.dao.ISimulatorTaskDao;
import org.jsbd.boss.dao.impl.SimulatorTaskResultDaoImpl;
import org.jsbd.boss.domian.channel.ClientTaskInfo;
import org.jsbd.boss.domian.channel.SimulatorTask;
import org.jsbd.boss.domian.channel.SimulatorTaskResult;
import org.jsbd.boss.service.ISimulatorTaskService;
import org.jsbd.boss.service.SimulatorManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 模拟器任务服务
 * @author <a href="mailto:zhouchao@xingbook.com" >周超</a>
 * @version 1.0.0
 */
@Service("simulatorTaskService")
public class SimulatorTaskServiceImpl implements ISimulatorTaskService {
	public static final Logger LOGGER = LoggerFactory.getLogger(SimulatorTaskServiceImpl.class);

	@Autowired
	private ISimulatorTaskDao simulatorTaskDao;
	
	@Autowired
	private SimulatorTaskResultDaoImpl simulatorTaskResultDao;
	
	@Resource
	private SimulatorManager simulatorManager;
	
	@Override
	public void saveSimulatorTaskAndResult(Map<String, Object> taskMap) {
		List<SimulatorTaskResult> resultList=new ArrayList<SimulatorTaskResult>();
		
		SimulatorTask task=null;
		
		for(Entry<String, Object> entity:taskMap.entrySet()){
			String uuid=entity.getKey();
			ClientTaskInfo taskInfo=(ClientTaskInfo)entity.getValue();
			SimulatorTaskResult taskResult=new SimulatorTaskResult();
			taskResult.setTaskId(taskInfo.getTaskId());
			taskResult.setUuid(uuid);
			resultList.add(taskResult);
			
			if(task==null){
				task=new SimulatorTask();
				task.setOrderType(taskInfo.getOrderType());
				if(taskInfo.getTaskContent()!=null){
					task.setTaskContent(taskInfo.getTaskContent().toJSONString());
				}
				task.setTaskId(taskInfo.getTaskId());
			}
		}
		
		simulatorTaskDao.save(task);
		simulatorTaskResultDao.save(resultList);
	}
	

	@Override
	public SimulatorTask getSimulatorTask(String taskId) {
		return simulatorTaskDao.getTaskByTaskId(taskId);
	}
}
